/**
 * A single cell on a Sudoku board. 
 * 
 * @author Theresa Wellington
 * @date April 2013
 *
 */
public class Cell {
	private boolean solved;
	private Object[] possibleValues;
	private int value;
	
	/**
	 * 
	 * @param cellValue 0 if cell is unsolved, or the value of the cell
	 * @param boardSize the size of the board (to populate possibleValues)
	 */
	public Cell(int cellValue, int boardSize){
		value = cellValue;
		if(cellValue!=0){
			//if the cell is solved, set solved to true
			solved = true;
		}
		else{
			//if the cell is unsolved, set solved to false and populate possible values array (all values possible at intial construction)
			solved = false;
			possibleValues = new Object[boardSize];
			for(Integer i=1; i<=boardSize; i++){
				possibleValues[i-1] = i;
			}
		}
	}
	
	/**
	 * Set solved to true and set the value for the cell
	 * @param value
	 */
	public void setSolved(int value){
		this.solved = true;
		this.value = value;
	}
	
	public boolean isSolved(){
		return solved;
	}
	
	public int getValue(){
		return value;
	}
	
	/**
	 * Sets the removeValue position in possibleValues to " "
	 * @param removeValue
	 */
	public void removePossibleValue(int removeValue){
		possibleValues[removeValue-1] = " ";
	}
	
	public Object[] getPossibleValues(){
		return possibleValues;
	}
	
	/**
	 * 
	 * @return a count of the number of possibleValues not set to " "
	 */
	public int getPossibleValuesCount(){
		int count = 0;
		for(int i=0; i<possibleValues.length; i++){
			if(!possibleValues[i].equals(" ")){
				count+=1;
			}
		}
		return count;
	}
	
	/**
	 * 
	 * @param value
	 * @return true if the value is present in possibleValues
	 */
	public boolean containsPossibleValue(Integer value){
		if(possibleValues != null){
			for(int i=0; i<possibleValues.length; i++){
				if(possibleValues[i].equals(value)){
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 
	 * @return 0 if there is more than one possible value, or the single possible value if there is only one
	 */
	public Integer getSinglePossibleValue(){
		if(possibleValues != null){
			if(getPossibleValuesCount()>1){
				return 0;
			}
			for(int i=0; i<possibleValues.length; i++){
				if(!possibleValues[i].equals(" ")){
					return (Integer) possibleValues[i];
				}
			}
		}
		return 0;
	}
	
	/**
	 * 
	 * @param boardSize
	 * @return the print string for the Cell, for use while solving (prints possible values for each cell)
	 */
	public String printPossibles(int boardSize){
		String returnString = "|";	
		if(solved){
			for(int i=0; i<boardSize-1; i++){
				returnString += " ";
			}
			returnString += value;
			for(int i=0; i<boardSize; i++){
				returnString += " ";
			}
		}
		else{
			for(int i=0; i<boardSize; i++){		
					returnString += possibleValues[i] + " ";
			}
			
		}

		returnString+="|";
		return returnString;
	}
}
